<?php
/**
 * This class will generate a list of values in html.
 *
 * @author Niels Janssen
 * @since 2008-07-09
 * @package Form.Elements
 */
class SelectOneChoice extends FormElement {

	/**
	 * The values this element should contain.
	 *
	 * @var String[][]
	 */
	private $listOfValues = array ();

	/**
	 * The identifier of the item that should be displayed as selected.
	 *
	 * @var String
	 */
	private $selected = '';

	/**
	 *
	 */
	//function __construct( $name, array $properties = array() ) {
	function __construct( $name, $label = '', $value = '', $class = '' ) {
		parent::__construct ( $name, array( 'value' => $value, 'label' => $label, 'class' => $class));
		//parent::__construct ( $name, $properties );
	}

	/**
	 * Adds one value to the list of values. The identifier will be what will be posted upon submit. It
	 * is recommended this identifier is unique.
	 *
	 * @param String $identifier
	 * @param String $value
	 */
	public function addValue( $identifier, $value ) {
		$this->listOfValues [$identifier] = $value;
	}

	/**
	 * Sets an element from the list of value as the default selected item using the identifier.
	 *
	 * @param String $identifier
	 */
	public function setSelected( $identifier ) {
		$this->selected = $identifier;
	}
	
	/**
	 * Return selected item
	 *
	 */
	public function getSelected() {
		return $this->selected;
	}	

	/**
	 * Adds an array of values to the list of values.
	 *
	 * @see SelectOneChoice::addValue();
	 *
	 * @param array $values
	 */
	public function addValues( array $values ) {
		foreach( $values as $identifier => $value ) {
			if ( is_array ( $value ) ) {
				$values = array_values ( $value );
			} else {
				$values = array ( $identifier, $value );
			}

			$this->addValue ( $values[0], $values[1] );
		}
	}

	/**
	 * @see FormNode::__toString()
	 */
	public function __toString( ) {
		//$select = '<div class="form select"><label for="' . ( $this->getFormName() != '' ? $this->getFormName() . '_' : '' ) . $this->name . '">' . $this->getProperty("label") . '</label><select name="' . ( $this->getFormName() != '' ? $this->getFormName() . '_' : '' ) . $this->name . '" id="' . ( $this->getFormName() != '' ? $this->getFormName() . '_' : '' ) . $this->name . '">' . "\r\n";
		$select = '<div class="'.( $this->getProperty("class") != '' ? $this->getProperty("class") : 'form inputtext' ).'"><label for="' . ( $this->getFormName() != '' ? $this->getFormName() . '_' : '' ) . $this->name . '">' . $this->getProperty("label") . '</label><select name="' . ( $this->getFormName() != '' ? $this->getFormName() . '_' : '' ) . $this->name . '" id="' . ( $this->getFormName() != '' ? $this->getFormName() . '_' : '' ) . $this->name . '">' . "\r\n";
		if( count ( $this->listOfValues ) > 0 ) {
			foreach( $this->listOfValues as $identifier => $value ) {
				$select .= "\t" . '<option value="' . $identifier . '"' . ( $identifier == $this->selected ? ' selected="selected"' : '' ) . '>' . $value . '</value>' . "\r\n";
			}
		}

		$select .= '</select>' . ( $this->inAJAXMode() || !empty( $this->errorMessage ) ? '<div class="error' . ( $this->inAJAXMode() ? ' ajax' : '' ) . '" id="' . ( $this->getFormName() != '' ? $this->getFormName() . '_' : '' ) . $this->name . '_error">' . $this->errorMessage . '</div>' : '' ) . '</div>' . "\r\n";

		return $select;
	}
}
